﻿using System;
using Microsoft.SPOT;
using TI_BLE_HCI_ClientLib.Controllers;
using Mountaineer.Netmf.Hardware;
using Microsoft.SPOT.Hardware;
using System.Text;
using System.Threading;

namespace YeelightBlueDemo
{
    public class Program
    {
        static ushort _connHandleYeelight1 = 0xFFFF, _connHandleYeelight2 = 0xFFFF, _connHandleYeelight3 = 0xFFFF;

        static bool _light1Enabled, _light2Enabled, _light3Enabled;

        static HCISimpleSerialCentralProfileController _controller = new HCISimpleSerialCentralProfileController(Socket3.SerialPortName, 115200);

        static OutputPort _ledR = new OutputPort(OnboardIO.LedRed, false);
        static OutputPort _ledG = new OutputPort(OnboardIO.LedGreen, false);
        static OutputPort _ledB = new OutputPort(OnboardIO.LedBlue, false);

        static InterruptPort _btn = new InterruptPort(OnboardIO.Button, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);

        public static void Main()
        {
            Debug.Print("YeelightBlue Demo started...");

            if (!initBLEAndConnect())
            {
                _ledR.Write(true);
                return;
            }

            _btn.OnInterrupt += _btn_OnInterrupt;

            while (true)
            {
                try
                {
                    doCycle();
                }
                catch {}
            }

        }

        private static void doCycle()
        {
            /*if (_light1Enabled) _controller.WriteCharacteristic(_connHandleYeelight1, 0x0025, getYeelightCommandString(0, 0, 0, 100));
            if (_light2Enabled) _controller.WriteCharacteristic(_connHandleYeelight2, 0x0025, getYeelightCommandString(0, 0, 0, 100));
            if (_light3Enabled) _controller.WriteCharacteristic(_connHandleYeelight3, 0x0025, getYeelightCommandString(0, 0, 0, 100));

            _ledR.Write(false);
            _ledG.Write(false);
            _ledB.Write(false);

            Thread.Sleep(1000);*/

            if (_light1Enabled) _controller.WriteCharacteristic(_connHandleYeelight1, 0x0025, getYeelightCommandString(255, 0, 0, 100));
            if (_light2Enabled) _controller.WriteCharacteristic(_connHandleYeelight2, 0x0025, getYeelightCommandString(255, 0, 0, 100));
            if (_light3Enabled) _controller.WriteCharacteristic(_connHandleYeelight3, 0x0025, getYeelightCommandString(255, 0, 0, 100));

            _ledR.Write(true);
            _ledG.Write(false);
            _ledB.Write(false);

            Thread.Sleep(1000);

            if (_light1Enabled) _controller.WriteCharacteristic(_connHandleYeelight1, 0x0025, getYeelightCommandString(255, 255, 0, 100));
            if (_light2Enabled) _controller.WriteCharacteristic(_connHandleYeelight2, 0x0025, getYeelightCommandString(255, 255, 0, 100));
            if (_light3Enabled) _controller.WriteCharacteristic(_connHandleYeelight3, 0x0025, getYeelightCommandString(255, 255, 0, 100));

            _ledR.Write(true);
            _ledG.Write(true);
            _ledB.Write(false);

            Thread.Sleep(1000);

            if (_light1Enabled) _controller.WriteCharacteristic(_connHandleYeelight1, 0x0025, getYeelightCommandString(0, 255, 0, 100));
            if (_light2Enabled) _controller.WriteCharacteristic(_connHandleYeelight2, 0x0025, getYeelightCommandString(0, 255, 0, 100));
            if (_light3Enabled) _controller.WriteCharacteristic(_connHandleYeelight3, 0x0025, getYeelightCommandString(0, 255, 0, 100));

            _ledR.Write(false);
            _ledG.Write(true);
            _ledB.Write(false);

            Thread.Sleep(1000);

            if (_light1Enabled) _controller.WriteCharacteristic(_connHandleYeelight1, 0x0025, getYeelightCommandString(0, 255, 255, 100));
            if (_light2Enabled) _controller.WriteCharacteristic(_connHandleYeelight2, 0x0025, getYeelightCommandString(0, 255, 255, 100));
            if (_light3Enabled) _controller.WriteCharacteristic(_connHandleYeelight3, 0x0025, getYeelightCommandString(0, 255, 255, 100));

            _ledR.Write(false);
            _ledG.Write(true);
            _ledB.Write(true);

            Thread.Sleep(1000);

            if (_light1Enabled) _controller.WriteCharacteristic(_connHandleYeelight1, 0x0025, getYeelightCommandString(0, 0, 255, 100));
            if (_light2Enabled) _controller.WriteCharacteristic(_connHandleYeelight2, 0x0025, getYeelightCommandString(0, 0, 255, 100));
            if (_light3Enabled) _controller.WriteCharacteristic(_connHandleYeelight3, 0x0025, getYeelightCommandString(0, 0, 255, 100));

            _ledR.Write(false);
            _ledG.Write(false);
            _ledB.Write(true);

            Thread.Sleep(1000);

            if (_light1Enabled) _controller.WriteCharacteristic(_connHandleYeelight1, 0x0025, getYeelightCommandString(255, 0, 255, 100));
            if (_light2Enabled) _controller.WriteCharacteristic(_connHandleYeelight2, 0x0025, getYeelightCommandString(255, 0, 255, 100));
            if (_light3Enabled) _controller.WriteCharacteristic(_connHandleYeelight3, 0x0025, getYeelightCommandString(255, 0, 255, 100));

            _ledR.Write(true);
            _ledG.Write(false);
            _ledB.Write(true);

            Thread.Sleep(1000);

            /*if (_light1Enabled) _controller.WriteCharacteristic(_connHandleYeelight1, 0x0025, getYeelightCommandString(255, 255, 255, 100));
            if (_light2Enabled) _controller.WriteCharacteristic(_connHandleYeelight2, 0x0025, getYeelightCommandString(255, 255, 255, 100));
            if (_light3Enabled) _controller.WriteCharacteristic(_connHandleYeelight3, 0x0025, getYeelightCommandString(255, 255, 255, 100));

            _ledR.Write(true);
            _ledG.Write(true);
            _ledB.Write(true);

            Thread.Sleep(1000);*/
        }

        static void _btn_OnInterrupt(uint data1, uint data2, DateTime time)
        {
            _controller.CloseConnection(0xFFFF);
        }

        static bool initBLEAndConnect()
        {
            _controller.Start();

            if (!_controller.Reset(HCIController.ResetTypeEnum.HardReset))
            {
                Debug.Print("BLE Module Reset FAILED!");
                return false;
            }

            _ledB.Write(true);


            if (!_controller.Init())
            {
                Debug.Print("BLE Module Initialization FAILED!");
                return false;
            }

            _ledB.Write(false);
            _ledG.Write(true);

            _light1Enabled = _controller.ConnectTo(new byte[] { 0x90, 0xD5, 0xEC, 0x30, 0x18, 0x00 }, out _connHandleYeelight1);

            if (!_light1Enabled) _controller.CloseConnection(0xFFFE);

            _light2Enabled = _controller.ConnectTo(new byte[] { 0x14, 0xDC, 0xEC, 0x30, 0x18, 0x00 }, out _connHandleYeelight2);

            if (!_light2Enabled) _controller.CloseConnection(0xFFFE);

            _light3Enabled = _controller.ConnectTo(new byte[] { 0x6E, 0xD7, 0xEC, 0x30, 0x18, 0x00 }, out _connHandleYeelight3);

            if (!_light3Enabled) _controller.CloseConnection(0xFFFE);

            if (!(_light1Enabled || _light2Enabled || _light3Enabled))
            {
                Debug.Print("BLE Connection to any Yeelight FAILED!");
                _ledB.Write(false);
                _ledG.Write(false);
                return false;
            }

            _ledB.Write(false);
            _ledG.Write(false);

            return true;
        }

        static string getYeelightCommandString(byte r, byte g, byte b, byte lum)
        {
            var s = r.ToString() + "," + g.ToString() + "," + b.ToString() + "," + lum.ToString();

            return s.PadRight(18, ',');
        }
    }
}
